<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3" xmlns:head="http://www.w3.org/1999/html">
<head th:include="public/public_css::public_css ('新增预期到货通知单')"/>
<%
%>
<body style="background-color: #f2f2f2;">
<div class="layui-fluid larry-wrapper">
    <div class="layui-card">
        <div class="layui-row layui-col-space30">
            <div class="layui-col-xs24">
                <form class="layui-form layui-card-header  layui-card-header-auto" id="searchForm">
                    <div class="layui-inline">
                        <label class="layui-form-label">ASN编号</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" id="asnbh" name="asnbh" th:value="${sj}"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">预计到货时间</label>
                        <div class="layui-input-inline">
                            <input type="text" class="layui-input" name="stime" id="stime"
                                   placeholder="yyyy-MM-dd HH:mm:ss"/>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">优先级</label>
                        <select id="yxj" name="yxj" lay-filter="aihao">
                            <option value="1">1</option>
                            <option value="2">2</option>
                            <option value="3">3</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">ASN类型</label>
                        <div class="layui-input-inline">
                            <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="selectId"
                                    name="telephone" select2 style="width: 193px"></select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">供应商编号</label>
                        <div class="layui-input-inline">
                            <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="gysId"
                                    name="telephone" select2 style="width: 193px"></select>
                        </div>
                    </div>
                    <div id="zd" style="display: none">
                        <div class="layui-inline">
                            <label class="layui-form-label">审核状态</label>
                            <div class="layui-input-inline">
                                <input type="text" class="layui-input" id="shzt" name="shzt" readonly="readonly"/>
                            </div>
                        </div>
                    </div>
                </form>

                <div class="group-button" id="toolbar">
                    <a class="layui-btn layui-btn-sm layui-btn-normal mx-btn">
                        <i class="layui-icon">&#x1005;</i> 新增明细
                    </a>
                    <a class="layui-btn layui-btn-sm layui-btn-normal save-btn">
                        <i class="layui-icon">&#x1005;</i> 保存
                    </a>
                </div>
                <!--列表-->
                <section class="panel panel-padding layui-card-body">
                    <div class="pro">
                        <table id="table1" class="table text-nowrap"></table>
                    </div>
                </section>
            </div>
        </div>
    </div>
</div>
</div>
<!--明细新增-->
<div id="addfunction" style="display: none">
    <form class="layui-form layui-card-header  layui-card-header-auto" id="form2">
        <div class="layui-inline">
            <label>货品编号</label>
            <div class="layui-input-inline">
                <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="goodsId"
                        name="hpbh" select2 style="width: 193px"></select>
            </div>
        </div>
        <div class="layui-inline">
            <label>货品名称</label>
            <div class="layui-input-inline">
                <input type="text" id="hpmc" name="hpmc" placeholder="货品名称" autocomplete="off" class="layui-input "
                       readonly="readonly"/>
            </div>
        </div>
        <div class="layui-inline">
            <label>规格</label>
            <div class="layui-input-inline">
                <input type="text" id="gg" name="gg" placeholder="规格" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>入库单位</label>
            <div class="layui-input-inline">
                <select lay-ignore="lay-ignore" class="js-data-example-ajax" id="rkdw"
                        name="rkdw" select2 style="width: 193px"></select>
            </div>
        </div>
        <br/>
        <div class="layui-inline">
            <label >货品批号</label>
            <div class="layui-input-inline">
                <input type="text" id="hpph" name="hpph" placeholder="货品批号" lay-verify="required" autocomplete="off"
                       class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>生产日期</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="scrq" id="scrq"
                       placeholder="yyyy-MM-dd HH:mm:ss" style="width: 193px"/>
            </div>
        </div>
        <div class="layui-inline">
            <label >失效日期</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" name="sxrq" id="sxrq"
                       placeholder="yyyy-MM-dd HH:mm:ss" disabled="disabled"/>
            </div>
        </div>
        <div class="layui-inline">
            <label>主单位（个）</label>
            <div class="layui-input-inline">
                <input type="text" id="yqsl" name="yqsl" placeholder="主单位数量" lay-verify="required" disabled="true"
                       autocomplete="off" class="layui-input "/>
            </div>
        </div>
        <br/>
        <div class="layui-inline">
            <label>体积（cm³）</label>
            <div class="layui-input-inline">
                <input type="text" id="tj" name="tj" placeholder="体积" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
                <span hidden id="tjS"></span>
            </div>
        </div>
        <div class="layui-inline">
            <label>净重（g）</label>
            <div class="layui-input-inline">
                <input type="text" id="jz" name="jz" placeholder="净重" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
                <span hidden id="jzS"></span>
            </div>
        </div>
        <div class="layui-inline">
            <label>毛重（g）</label>
            <div class="layui-input-inline">
                <input type="text" id="zl" name="zl" placeholder="毛重" readonly="readonly" autocomplete="off"
                       class="layui-input " style="width: 193px"/>
                <span hidden id="zlS"></span>
            </div>
        </div>
        <div class="layui-inline">
            <label>箱单位（xx箱xx件）</label>
            <div class="layui-input-inline">
                <input type="text" id="xdwsl" name="xdwsl" placeholder="箱单位数量" lay-verify="required" disabled="true"
                       autocomplete="off" class="layui-input " style="width: 193px"/>
            </div>
        </div>
        <br/>
        <div class="layui-inline">
            <label>金额(元)</label>
            <div class="layui-input-inline">
                <input type="text" id="qian" name="qian" placeholder="金额" readonly="readonly" autocomplete="off"
                       class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>总金额(元)</label>
            <div class="layui-input-inline">
                <input type="text" id="je" name="je" placeholder="总金额" lay-verify="required" th:value="0"
                       autocomplete="off" class="layui-input "/>
            </div>
        </div>
        <div class="layui-inline">
            <label>备注</label>
            <div class="layui-input-inline">
                <input type="text" id="bz" name="bz" placeholder="备注" lay-verify="required" autocomplete="off"
                       class="layui-input " style="width: 400px;"/>
            </div>
        </div>


    </form>
</div>
</body>
<div th:include="public/public_js::public_js"></div>
<script>
    var $table;
    var hpId;
    var UnitNum;
    var baozhuangId;//包装id
    var rkdwId;//声明一个全局变量的入库单位id
    var zname;//声明主单位名称
    layui.use(['list', 'layer', 'zzpUtils', 'jquery', 'laydate'], function () {
        window.operateEvent1 = {

            "click #btn_bj": function (e, value, row, index) {//编辑
                update_order(row);
            },
            "click #btn_sc": function (e, value, row, index) {//删除
                del_order(index, row.idex);
            }
        }
        var zzpUtils = layui.zzpUtils,
            $ = layui.jquery,
            laydate = layui.laydate;
        laydate.render({
            elem: '#stime'
            , theme: '#20a0ff'
            , range: '~'//练起来
        });
        laydate.render({
            elem: '#scrq'
            , theme: '#20a0ff'
        });
        laydate.render({
            elem: '#sxrq'
            , theme: '#20a0ff'
        })

        //修改明细行
        function update_order(row) {
            layer.open({
                type: 1,
                title: "编辑",
                content: $("#addfunction"),
                area: ['70%', '80%'],
                success: function (layero, index) {
                    $('#goodsId').html('<option value="' + row.hpid + '">' + row.bh + '</option>').trigger("change");
                    $('#rkdw').html('<option value="' + row.zdwid + '">' + row.ms + '</option>').trigger("change");
                    $('#hpmc').val(row.hpmc);
                    $('#gg').val(row.gg);
                    $('#tj').val(row.tj);
                    $('#jz').val(row.jz);
                    $('#zl').val(row.zl);
                    $('#qian').val(row.qian);
                    $('#sxrq').val(row.sxrq);
                    $('#yqsl').val(row.yqsl);
                    $('#xdwsl').val(row.xdwsl);
                    $('#je').val(row.je);
                    $('#bz').val(row.bz);
                    $('#scrq').val(row.scrq);
                    $('#hpph').val(row.hppc);
                },
                btn: ['保存', '取消'],
                btn1: function (index, layero) {
                    //保存前判断
                    var rows = $table.bootstrapTable('getData');
                    var hpid = $("#goodsId").select2("val");
                    var zdwid =rkdwId;
                    var yqsl = $('#yqsl').val();
                    var hpbh = $("#goodsId").select2("data")[0].text;
                    var hppc = $('#hpph').val();
                    var time = $('#scrq').val();
                    var number = 0;
                    if (rows.length > 0) {
                        for (var i = 0; i < rows.length; i++) {
                            if (row.bh != rows[i].bh || row.hppc != rows[i].hppc || row.scrq != rows[i].scrq) {
                                if (rows[i].bh == hpbh && rows[i].hppc == hppc && rows[i].scrq == time) {
                                    number++;
                                }
                            }
                        }
                    }
                    if (number > 0) {
                        layer.msg("发现 完全相同的 货品编号" + hpbh + " 货品批次" + hppc + " 生产日期" + time + " 已经存在！");
                    } else if (zdwid == null || hpid == null || yqsl == '') {
                        layer.msg("请将表格信息完善");
                    } else {
                        var pass = {};
                        pass.hpid = hpid;
                        pass.bh = hpbh;
                        pass.zdwid = zdwid;
                        pass.ms = zname;
                        pass.hpmc = $('#hpmc').val();
                        pass.gg = $('#gg').val();
                        pass.hppc = hppc;
                        pass.scrq = time;
                        pass.yqsl = yqsl;
                        pass.xdwsl = $('#xdwsl').val();
                        pass.sxrq = $('#sxrq').val();
                        pass.bz = $('#bz').val();
                        pass.tj = $('#tj').val();
                        pass.jz = $('#jz').val();
                        pass.zl = $('#zl').val();
                        pass.je = $('#je').val();
                        pass.qian = $('#qian').val();
                        pass.pssl = 0;
                        pass.jssl = 0;
                        pass.dycgmxid = 0;
                        $table.bootstrapTable('updateByUniqueId', {id: row.idex, row: pass});
                        //保存完成后 清空
                        $("#goodsId").select2("val", " ");
                        $("#rkdw").select2("val", " ");
                        $('#hpmc').val('');
                        $('#gg').val('');
                        $('#tj').val('');
                        $('#jz').val('');
                        $('#zl').val('');
                        $('#qian').val('');
                        $('#sxrq').val('');
                        $('#yqsl').val('');
                        $('#xdwsl').val('');
                        $('#je').val('');
                        $('#bz').val('');
                        $('#scrq').val(getNowFormatDate());
                        $('#hpph').val(getNowFormatDate1());
                        layer.close(index);
                    }
                }
                , btn2: function (index, layero) {
                    $("#goodsId").select2("val", " ");
                    $("#rkdw").select2("val", " ");
                    $('#hpmc').val('');
                    $('#gg').val('');
                    $('#tj').val('');
                    $('#jz').val('');
                    $('#zl').val('');
                    $('#qian').val('');
                    $('#sxrq').val('');
                    $('#yqsl').val('');
                    $('#xdwsl').val('');
                    $('#je').val('');
                    $('#bz').val('');
                    $('#scrq').val(getNowFormatDate());
                    $('#hpph').val(getNowFormatDate1());
                    layer.close(index);
                }
            })
        }

        //删除
        function del_order(index, idex) {
            var ids = [];
            ids.push(idex);
            $table.bootstrapTable('remove', {field: 'idex', values: ids});
        }

        /*select2单选
        * ASN类型
        * */
        zzpUtils.select2({
            elem: "#selectId",
            url: "/wmsPurchaseOrder/getSelectLx",
            minimumInputLength: 0
        });
        /*
        * 供应商编号
        * */
        $('#gysId').select2({
            ajax: {
                url: '/zxsdwms/wmsPurchaseOrder/getGysmc',
                dataType: 'json',
                type: 'POST',
                contentType: "application/json",
                data: function (params) {
                    var query = {
                        bh: params.term
                    };
                    return JSON.stringify(query);
                },
                processResults: function (data) {
                    var arr = new Array();
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            arr.push({id: data[i].id, text: data[i].mc});
                        }
                    }
                    return {
                        results: arr
                    };
                },
                cache: true
            },
            language: "zh-CN",
            placeholder: '请选择供应商',
            allowClear: true
        });

        /*ASN类型值改变事件*/
        $("#selectId").on("select2:select", function (e) {
            var id = $(this).val();
            $.ajax({
                url: "/zxsdwms/wmsEstimatedArrivalOrder/getShzt",       //请求地址
                type: "POST",            //请求方式
                data: {id: id},    //请求参数
                success: function (data) {
                    if (data == '0') {//是否自动审核 0不自动 1自动
                        $("#zd").show();
                        $("#shzt").val("不自动");
                    } else {
                        $("#zd").show();
                        $("#shzt").val("自动");
                    }
                }
            });
        })
        $table = zzpUtils.table({
            elem: '#table1',
            isApi: true,
            uniqueId: "idex",
            pagination: true,
            showColumns: false, //是否显示所有的列
            //url: '',
            columns: [{
                field: 'idex',
                title: 'id',
                visible: false
            }, {
                field: 'dycgmxid',
                title: '对应采购明细ID',
                visible: false
            }, {
                field: 'qian',
                title: '钱',
                visible: false
            }, {
                field: 'hpid',
                title: '货品id',
                visible: false
            }, {
                field: 'zdwid',
                title: '主单位id',
                visible: false,
            }, {
                field: 'bh',
                title: '货品编号'
            }, {
                field: 'hpmc',
                title: '货品名称'
            }, {
                field: 'gg',
                title: '规格'
            }, {
                field: 'nc',
                title: 'NC编码'
            }, {
                field: 'ms',
                title: '主单位'
            }, {
                field: 'hppc',
                title: '货品批次'
            }, {
                field: 'scrq',
                title: '生产日期'
            }, {
                field: 'yqsl',
                title: '主单位数量'
            }, {
                field: 'xdwsl',
                title: '箱单位数量'
            }, {
                field: 'sxrq',
                title: '失效日期'
            }, {
                field: 'pssl',
                title: '破损数量'
            }, {
                field: 'jssl',
                title: '拒收数量'
            }, {
                field: 'bz',
                title: '备注'
            }, {
                field: 'tj',
                title: '体积'
            }, {
                field: 'jz',
                title: '净重'
            }, {
                field: 'zl',
                title: '毛重'
            }, {
                field: 'je',
                title: '金额'
            }, {
                field: 'caozuo',
                title: '操作',
                events: operateEvent1,
                formatter: operateFormatter1
            }],
            detailFormatter: function (index, row) {
                return "这里就没有确定过11";
            }
        });

        /*表格右侧按钮*/
        function operateFormatter1(value, row, index) {

            return [
                '<a class="layui-btn layui-btn-xs layui-btn-normal" id="btn_bj">编辑</a>',
                '<a class="layui-btn layui-btn-xs layui-btn-normal" id="btn_sc">删除</a>'
            ].join('')
        }

        //添加明细
        $(".mx-btn").on('click', function () {
            hpId=0;
            var aid = $("#selectId").select2("val");
            var gid = $("#gysId").select2("val");
            if (gid != null && aid != null) {
                $('#scrq').val(getNowFormatDate());
                $('#hpph').val(getNowFormatDate1());
                var num = parseInt(Math.random() * 100) + 1;
                layer.open({
                    type: 1,
                    title: "新增",
                    content: $("#addfunction"),
                    area: ['70%', '80%'],
                    btn: ['保存', '取消'],
                    btn1: function (index, layero) {
                        //保存前判断
                        var rows = $table.bootstrapTable('getData');
                        var hpid = $("#goodsId").select2("val");
                        var zdwid = rkdwId;
                        var yqsl = $('#yqsl').val();
                        var hpbh = $("#goodsId").select2("data")[0].text;
                        var hppc = $('#hpph').val();
                        var time = $('#scrq').val();
                        var number = 0;
                        if (rows.length > 0) {
                            for (var i = 0; i < rows.length; i++) {
                                if (rows[i].bh == hpbh && rows[i].hppc == hppc && rows[i].scrq == time) {
                                    number++;
                                }
                            }
                        }
                        if (number > 0) {
                            layer.msg("发现 完全相同的 货品编号" + hpbh + " 货品批次" + hppc + " 生产日期" + time + " 已经存在！");
                        } else if (hpid == null || zdwid == null || yqsl == '') {
                            layer.msg("请将表格信息完善");
                        } else {
                            var pass = {};
                            pass.idex = num;
                            pass.hpid = hpid;
                            pass.bh = hpbh;
                            pass.zdwid = zdwid;
                           // pass.ms = $("#rkdw").select2("data")[0].text;
                            pass.ms=zname;
                            pass.hpmc = $('#hpmc').val();
                            pass.gg = $('#gg').val();
                            pass.hppc = hppc;
                            pass.scrq = time;
                            pass.yqsl = yqsl;
                            pass.xdwsl = $('#xdwsl').val();
                            pass.sxrq = $('#sxrq').val();
                            pass.bz = $('#bz').val();
                            pass.tj = $('#tj').val();
                            pass.jz = $('#jz').val();
                            pass.zl = $('#zl').val();
                            pass.je = $('#je').val();
                            pass.qian = $('#qian').val();
                            pass.pssl = 0;
                            pass.jssl = 0;
                            pass.dycgmxid = 0;
                            $table.bootstrapTable('insertRow', {index: num, row: pass});
                            //保存完成后 清空
                            $("#goodsId").select2("val", " ");
                            $("#rkdw").select2("val", " ");
                            $('#hpmc').val('');
                            $('#gg').val('');
                            $('#tj').val('');
                            $('#jz').val('');
                            $('#zl').val('');
                            $('#qian').val('');
                            $('#sxrq').val('');
                            $('#yqsl').val('');
                            $('#xdwsl').val('');
                            $('#je').val('');
                            $('#bz').val('');
                            $('#scrq').val(getNowFormatDate());
                            $('#hpph').val(getNowFormatDate1());
                           /* $("#rkdw").attr("disabled",true);*/
                            layer.close(index);
                        }
                    }
                    , btn2: function (index, layero) {
                       /* $("#rkdw").attr("disabled",true);*/
                        $("#goodsId").select2("val", " ");
                        $("#rkdw").select2("val", " ");
                        $('#hpmc').val('');
                        $('#gg').val('');
                        $('#tj').val('');
                        $('#jz').val('');
                        $('#zl').val('');
                        $('#qian').val('');
                        $('#sxrq').val('');
                        $('#yqsl').val('');
                        $('#xdwsl').val('');
                        $('#je').val('');
                        $('#bz').val('');
                        $('#scrq').val(getNowFormatDate());
                        $('#hpph').val(getNowFormatDate1());
                        hpId=0;
                        layer.close(index);
                    }
                })
            } else {
                layer.msg("请先填写上面信息！");
            }
        })

        //保存预期到货通知单
        $(".save-btn").on('click', function () {
            //判断上面的值是否为空
            var $rows = $table.bootstrapTable('getData');
            var asnbh = $("#asnbh").val();//ASN编号
            var stime = $("#stime").val();//预计到货时间
            var yxj = $("#yxj").val();//优先级
            var selectId = $("#selectId ").select2("val");//ASN类型
            var gysId = $("#gysId ").select2("val");//供应商
            var shzt = $("#shzt").val();//审核状态
            if (asnbh == '' || stime == '' || selectId == null || gysId == null) {
                layer.msg("请完善数据！");
            } else {
                var arr = stime.split("~");
                var pass1 = {};
                pass1.asnbh = asnbh;
                pass1.yxj = yxj;
                pass1.asnlx = selectId;
                pass1.gysid = gysId;
                pass1.locking = -1;
                pass1.gysmc = $("#gysId").select2("data")[0].text;
                pass1.yjdhsjks = arr[0];
                pass1.yjdhsjjs = arr[1];
                pass1.detailed = $rows;
                if ($rows.length > 0) {
                    zzpUtils.confirm("是否确定新增", function (index) {
                        zzpUtils.action({
                            url: "/wmsEstimatedArrivalOrder/add",
                            data: JSON.stringify(pass1),
                            success: function (data) {
                                setTimeout(function () {
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                    if (data.type == 'success') {
                                        top.layer.msg(data.title);
                                    } else {
                                        top.layer.msg(data.message);
                                    }
                                }, 500);
                                zzpUtils.refreshTable("#table");
                            }, error: function (xhr, textStatus) {
                                console.log(textStatus);
                                console.log(xhr)
                            }
                        })
                    })
                    return false;

                } else {
                    layer.msg("请填写明细");
                }
            }
        })
        /*******************************[明细操作]*************************************/
        //select2查询商品 xxx
        $('#goodsId').select2({
            ajax: {
                url: '/zxsdwms/wmsPurchaseOrder/getGoods',
                dataType: 'json',
                type: 'POST',
                contentType: "application/json",
                data: function (params) {
                    var query = {
                        bh: params.term,
                        gysid: $("#gysId").select2("val")
                    };
                    return JSON.stringify(query);
                },
                processResults: function (data) {
                    var arr = new Array();
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            arr.push({id: data[i].goodsId, text: data[i].goodsBh});
                        }
                    }
                    return {
                        results: arr
                    };
                },
                cache: true
            },
            language: "zh-CN",
            placeholder: '请选择货品编号',
            allowClear: true
        });
        //货品选择获取货品id
        $("#goodsId").on("select2:select", function (e) {
            var id = $(this).val();
            hpId=id;
            $.ajax({
                url: "/zxsdwms/wmsEstimatedArrivalOrder/getOneGood",       //请求地址
                type: "POST",            //请求方式
                data: {id: id},    //请求参数
                success: function (data) {
                    $("#hpmc").val(data.goodsName);
                    $("#gg").val(data.gg);
                    $("#sxrq").val(transferCouponValueTime($('#scrq').val(), data.yxq));//失效日期
                    baozhuangId=data.bzid;
                }
            });
            //清空原来入库单位选择的值
            $("#rkdw").select2("val", " ");
            $('#tj').val('');
            $('#jz').val('');
            $('#zl').val('');
            $('#qian').val('');
        });
        //select2查询入库单位
        $('#rkdw').select2({
            ajax: {
                url: '/zxsdwms/wmsEstimatedArrivalOrder/getRkdw',
                dataType: 'json',
                type: 'POST',
                contentType: "application/json",
                data: function (params) {
                    var query = {
                        hpid: hpId
                    };
                    return JSON.stringify(query);
                },
                processResults: function (data) {
                    var arr = new Array();
                    if (data) {
                        for (var i = 0; i < data.length; i++) {
                            arr.push({id: data[i].id, text: data[i].ms});
                        }
                    }
                    return {
                        results: arr
                    };
                },
                cache: true
            },
            language: "zh-CN",
            placeholder: '请选择入库单位',
            allowClear: true
        });

        //入库单位触发事件
        $("#rkdw").on("select2:select", function (e) {
            var id = $(this).val();
            //获取包装信息
            var warehouseunit = wareHouseUnit(baozhuangId);
            for(var i=0;i<warehouseunit.length;i++){
                if(warehouseunit[i].bzlx=='0'){
                    rkdwId=warehouseunit[i].id;
                    zname=warehouseunit[i].ms;
                }
                if(warehouseunit[i].id==id){
                    var asl=$('#yqsl').val();
                    $("#tj").val(warehouseunit[i].tj*asl);
                    $("#jz").val(warehouseunit[i].jz*asl);
                    $("#zl").val(warehouseunit[i].zl*asl);
                    $("#qian").val(warehouseunit[i].je);
                    $("#tjS").val(warehouseunit[i].tj);
                    $("#jzS").val(warehouseunit[i].jz);
                    $("#zlS").val(warehouseunit[i].zl);
                    // 主单位数量
                    UnitNum = warehouseunit[i].zdwsl;
                    if (warehouseunit[i].bzlx == '0') {//主单位

                        $('#yqsl').attr("disabled", false);
                        $('#xdwsl').attr("disabled", true);

                    } else {//箱单位

                        $('#yqsl').attr("disabled", true);
                        $('#xdwsl').attr("disabled", false);
                    }
                }
            }
           /* $.ajax({
                url: "/zxsdwms/wmsEstimatedArrivalOrder/getBzlx",
                type: "POST",            //请求方式
                data: {id: id},    //请求参数
                success: function (data) {
                }
            })*/
        });
        // 查询入库单位
        function wareHouseUnit(id) {
            var DATA;
            $.ajax({
                url: '/zxsdwms/wmsPurchaseOrder/packDetailList/' + id,
                type: 'GET',
                dataType: 'json',
                contentType: 'application/json',
                async: false,
                success: function (data) {
                    DATA = data;
                }
            })
            return DATA;
        }

        // 监听主单位数量
        $('#yqsl').on('input propertychange', function (data) {
            var value = data.currentTarget.value;
            var len, lenfloat;
            var volumeS = parseFloat($('#jzS').val());//净重
            var suttleS = parseFloat($('#zlS').val());//毛重
            var grossWeightS = parseFloat($('#tjS').val());//体积
            var money = parseFloat($('#qian').val());//金额
            // 如果value不为空
            if (value && value !== '0') {
                len = parseInt(value);
                lenfloat = parseFloat(value);
                if (value.indexOf('-') >= 0) {
                    $('#yqsl').val('');
                } else if ((lenfloat - len) > 0) {
                    $('#yqsl').val(len);
                } else {
                    $('#jz').val(toFixeds(volumeS * len));
                    $('#zl').val(toFixeds(suttleS * len));
                    $('#tj').val(toFixeds(grossWeightS * len));
                    $('#je').val(toFixeds(money * len));

                    // 如果主单位数量为1
                    if (UnitNum === 1) {
                        $('#xdwsl').val(len.toString() + '箱0件');
                    } else {
                        var BOX, PIECE;
                        BOX = parseInt(len / UnitNum);
                        PIECE = len % UnitNum;
                        console.log('BOX', UnitNum, BOX, PIECE);
                        $('#xdwsl').val(BOX + '箱' + PIECE + '件');
                    }
                }
            } else {
                $('#jz').val(volumeS);
                $('#zl').val(suttleS);
                $('#tj').val(grossWeightS);
                $('#je').val(0);
                $('#xdwsl').val('0箱0件');
            }
        })
        //箱单位离开事件
        $('#xdwsl').blur(function () {
            var value = $('#xdwsl').val();
            var a = value.indexOf('箱');
            var b = value.indexOf('件');
            var c = value.charAt(value.length - 1);
            if (a > 0 && b > 0 && c == '件') {

            } else {
                layer.msg("必须符合 xx箱xx件 的格式!");
            }
        });
        // 监听箱单位数量
        $('#xdwsl').on('input propertychange', function (data) {
                var value, indbox, indpiece, box, piece, boxs, pieces;
                var volumeS = parseFloat($('#jzS').val());//净重
                var suttleS = parseFloat($('#zlS').val());//毛重
                var grossWeightS = parseFloat($('#tjS').val());//体积
                var money = parseFloat($('#qian').val());
                value = data.currentTarget.value;
                indbox = value.indexOf('箱');
                indpiece = value.indexOf('件');
                if (indbox > 0 && indpiece > 0) {
                    box = value.substring(0, indbox);
                    piece = value.substring(indbox + 1, indpiece);
                    var unitNumber = parseInt(box) * UnitNum + parseInt(piece);
                    var len;
                    if (unitNumber !== 0) {
                        len = unitNumber;
                        $('#je').val(toFixeds(money * len));
                    } else {
                        len = 1;
                        $('#je').val(0);
                    }
                    $('#yqsl').val(unitNumber);
                    $('#jz').val(toFixeds(volumeS * len));
                    $('#zl').val(toFixeds(suttleS * len));
                    $('#tj').val(toFixeds(grossWeightS * len));
                }
            }
        )

        //获取当前日期
        function getNowFormatDate() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + seperator1 + month + seperator1 + strDate;
            return currentdate;
        }

        function getNowFormatDate1() {
            var date = new Date();
            var seperator1 = "-";
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var strDate = date.getDate();
            if (month >= 1 && month <= 9) {
                month = "0" + month;
            }
            if (strDate >= 0 && strDate <= 9) {
                strDate = "0" + strDate;
            }
            var currentdate = year + month + strDate;
            return currentdate;
        }

        /**
         * 优惠券有效期
         * startDate:起始日期
         * valueTime：有效天数
         */
        function transferCouponValueTime(startDate, valueTime) {

            var date = new Date(startDate);
            var newDate = new Date(date.getFullYear(), date.getMonth(), Number(date.getDate()) + Number(valueTime));
            var year1 = date.getFullYear();
            var month1 = date.getMonth() + 1;
            var day1 = date.getDate();
            var year2 = newDate.getFullYear();
            var month2 = newDate.getMonth() + 1;
            var day2 = newDate.getDate();
            /* return "<span>"+year1+"</span>-<span>"+month1+"</span>-<span>"+day1+"</span>一<span>"
                 +year2+"</span>-<span>"+month2+"</span>-<span>"+day2+"</span>";*/

            return year2 + "-" + month2 + "-" + day2;
        }

        function toFixeds(data) {
            var str = "";
            if (data) {
                data = data.toString();
                if (data.indexOf('.') < 0) {
                    str = data;
                } else {
                    str = data.substring(0, data.indexOf('.') + 3);
                }
                return parseFloat(str);
            }
        }


    });

</script>
</html>